package tq.com.tqcom.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import tq.com.systools.system.StringHelper;
import tq.com.tqcom.R;

public class TQClickButton extends LinearLayout {
    private ImageView imageView;
    private TextView textView;

    private int normalImage = R.drawable.icon_fabu;
    private int highLigntImage = R.drawable.icon_fabu_sel;

    public TQClickButton(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0, 0);
    }

    public TQClickButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);


        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TQClickButton);
        int ori = typedArray.getInt(R.styleable.TQClickButton_TQClickButton_ori, 0);
        if(ori == 0){
            LayoutInflater.from(context).inflate(R.layout.layout_tqclickbutton_v, this);
        }else{

        }

        imageView = this.findViewById(R.id.tq_clickbutton_image);
        textView = findViewById(R.id.tq_clickbutton_text);

        normalImage = typedArray.getResourceId(R.styleable.TQClickButton_TQClickButton_icon, normalImage);
        imageView.setImageResource(normalImage);

        String text = typedArray.getString(R.styleable.TQClickButton_TQClickButton_name);
        if(StringHelper.isEmpty(text)){
            text = "title";
        }
        textView.setText(text);

        highLigntImage = typedArray.getResourceId(R.styleable.TQClickButton_TQClickButton_icon_sel, highLigntImage);
        this.seleted = typedArray.getBoolean(R.styleable.TQClickButton_TQClickButton_sel, false);
        if(isSeleted()){
            imageView.setImageResource(highLigntImage);
        }
        typedArray.recycle();
    }

    private boolean seleted;
    public void setSelected(boolean selected){
        this.seleted = selected;
        imageView.setImageResource(this.seleted ? this.highLigntImage : this.normalImage);
    }

    public boolean isSeleted(){return  seleted;}

    private int index = -1;
    public void setIndex(int i){
        index = i;
    }
    public int getIndex(){return index;}
}
